import router from "@/router";
import axios from 'axios'

export default class WaybillValidator {
  async sign ({ waybillId }, $vue) {
    const signUrl = `/transport/waybill/sign`;
    let response = await axios.post(signUrl, { waybillId });
    if (0 == response.code) {
      $vue.$message({ type: 'success', message: response.message });
      $vue.search();
    }
  }

  async validate ({ type,
    closeStatus,
    billDayStatus,
    routerName, waybillId, carrier, signExceptionStatus,
    pickCovenant,
    transportStatus, trackType,
    selection
  }, $vue) {
    const close = [10, 20, 50, 60];

    switch (type) {
      case 'sign':
        if (close.includes(closeStatus)) {
          $vue.$message({ type: 'warning', message: `运单有关单待办需要处理，暂不能操作` })
          return Promise.reject(new Error(`运单有关单待办需要处理，暂不能操作`))
        }
        // 签收前置
        if ([1, 3].includes(trackType)) {
          let response = await axios.post(`/transport/waybill/sign/check`, { waybillId })
            .catch(res => {
              return $vue.$confirm(`${response?.message}`, "提示", {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
                type: "warning"
              }).then(() => {
                this.sign({ waybillId }, $vue);
                return Promise.reject(new Error("运单没到目的地"))
              });
            })
          if (response.code != 0) {
            return $vue.$confirm(`${response?.message}`, "提示", {
              confirmButtonText: "确认",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.sign({ waybillId }, $vue);
              return Promise.reject(new Error("运单没到目的地"))
            });
          }
        }
        break;

      case 'batchSign':
        if (selection?.length === 0) {
          $vue.$message({ type: 'warning', message: "请选择至少一条记录" })
          return Promise.reject(new Error("请选择至少一条记录"))
        }
        // try {
        //   selection.forEach(function (item) {
        //     if (close.includes(item.closeStatus)) {
        //       $vue.$message({ type: 'warning', message: `运单有关单待办需要处理，暂不能操作` })
        //       throw Error();
        //     }
        //   });
        // } catch (e) {
        //   return Promise.reject(new Error(`运单有关单待办需要处理，暂不能操作`))
        // }
        break;

      case 'receipt':
        if (signExceptionStatus) {
          return $vue.$confirm("运单有异常，是否进入详情页确认？", "提示", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            router.push({
              name: routerName,
              query: {
                waybillId
              }
            });
          });
        }
        break;

      case 'batchReceipt':
        if (selection?.length === 0) {
          $vue.$message({ type: 'warning', message: "请选择至少一条记录" })
          return Promise.reject(new Error("请选择至少一条记录"))
        }

        // let invalidArrs = [];
        // selection.map(item => {
        //   if (item.signExceptionStatus) {
        //     invalidArrs.push(item.contractNo);
        //   }
        // })
        // if (invalidArrs.length === selection.length) {
        //   return $vue.$confirm(`所选运单全部有异常，不能进行批量回单。`,
        //     "提示",
        //     {
        //       confirmButtonText: "我知道了",
        //       type: "warning",
        //       showClose: false,
        //       showCancelButton: false,
        //       closeOnClickModal: false
        //     }).then(() => {
        //       return Promise.reject(new Error("所选运单全部有异常，不能进行批量回单。"))
        //     });
        // }
        // if (invalidArrs.length) {
        //   let invalid = invalidArrs.map(item => `"${item}"`);
        //   let tips = invalid.toString().replace(new RegExp(',', 'g'), '，');
        //   return $vue.$confirm(`运单中有异常，异常运单：${tips}，可提交正常运单，是否执行？`,
        //     "提示",
        //     {
        //       confirmButtonText: "是",
        //       cancelButtonText: "否",
        //       type: "warning",
        //       closeOnClickModal: false
        //     }).then(() => {
        //       return Promise.resolve(true)
        //     });
        // }
        break;

      case 'dispatch':
        if (close.includes(closeStatus)) {
          $vue.$message({ type: 'warning', message: `运单有关单待办需要处理，暂不能操作` })
          return Promise.reject(new Error(`运单有关单待办需要处理，暂不能操作`))
        }
        if (billDayStatus) {
          return $vue.$confirm(`运单已过关账期，双方不能再报价，是否确定继续操作?`,
            "提示",
            {
              confirmButtonText: "调度",
              type: "warning",
              closeOnClickModal: false
            }).then(() => {
              return Promise.resolve(true)
            });
        }
        break;

      case 'batchDispatch':
        if (selection?.length === 0) {
          $vue.$message({ type: 'warning', message: "请选择至少一条记录" })
          return Promise.reject(new Error("请选择至少一条记录"))
        }
        // try {
        //   selection.forEach(function (item) {
        //     if (close.includes(item.closeStatus)) {
        //       console.log('item.closeStatus', item.closeStatus)
        //       $vue.$message({ type: 'warning', message: `运单有关单待办需要处理，暂不能操作` })
        //       throw Error();
        //     }
        //   });
        // } catch (e) {
        //   return Promise.reject(new Error(`运单有关单待办需要处理，暂不能操作`))
        // }

        let invalidArr = [];
        selection.map(item => {
          if (item.billDayStatus) {
            invalidArr.push(item.contractNo);
          }
        })
        if (invalidArr.length) {
          return $vue.$confirm(`运单(合同号${invalidArr.toString()})已过关账期，双方不能再报价，是否确定继续操作?`,
            "提示",
            {
              confirmButtonText: "调度",
              type: "warning",
              closeOnClickModal: false
            }).then(() => {
              return Promise.resolve(true)
            });
        }
        break;

      case 'pickCoenant':
        console.log("pickCovenant", pickCovenant)
        if (pickCovenant && !pickCovenant?.arrivalTime) {
          return $vue.$confirm(`进场时间不能为空，请填写后确认`,
            "提示",
            {
              confirmButtonText: "我知道了",
              type: "warning",
              showClose: false,
              showCancelButton: false,
              closeOnClickModal: false
            }).then(() => {
              return Promise.reject(new Error("进场时间不能为空，请填写后确认"))
            });
        }
        break;
    }
  }
}